package org.servitehs.ServiteStudents;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.text.format.Time;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ServiteStudentsActivity extends Activity {
    /** Called when the activity is first created. */
	
	WebView webViewEin; // Main WebView
	int lat, lon;
	LocationManager lm;
	String tower, currentlat, currentlon;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        webViewEin = (WebView) findViewById(R.id.webViewEin);
        try {
        	webViewEin.loadUrl("http://www.webstreetlabs.com/servite/mobile/webapp/index.html"); // Load Site
        } catch (Exception e) {
        	e.printStackTrace();
        }
        webViewEin.setWebViewClient(new mainView());
        webViewEin.getSettings().setJavaScriptEnabled(true); // Never delete
        
        webViewEin.getSettings().setDomStorageEnabled(true); // Allow cache of the site for offline use

	    webViewEin.getSettings().setAppCacheMaxSize(1024*1024*8);
	    
	    String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
	    webViewEin.getSettings().setAppCachePath(appCachePath);
	    webViewEin.getSettings().setAllowFileAccess(true);
	    webViewEin.getSettings().setAppCacheEnabled(true);
	    

	    // Start Location 
	    try {
	       // Get location
	        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	        Criteria crit = new Criteria();
	        tower = lm.getBestProvider(crit, false);
	        Location location = lm.getLastKnownLocation(tower);
	        
	        if(location != null) {
	        	lat = (int) (location.getLatitude() *1E6);
	        	lon = (int) (location.getLongitude() *1E6);
	        	currentlat = lat + "";
	        	currentlon = lon + "";
	        	currentlat = currentlat.substring(0, 2) + "." + currentlat.substring(2);
	        	currentlon = currentlon.substring(0, 4) + "." + currentlon.substring(4);
	
		        if (lat == 33.845656) {
		        	if(lon == -117.949347) {
		
			        	int hour = Time.HOUR;
			        	
			        	if((hour >= 7 ) && (hour >= 3)) {
			        		Dialog a = new Dialog(this);
			            	a.setTitle("School");
			            	LinearLayout y = new LinearLayout(this);
			            	y.setGravity(Gravity.CENTER);
			            	TextView tv = new TextView(this);
			            	tv.setText("Your at Servite in school hours.\n Would you like to silence your phone?");
			            	Button b = new Button(this);
			            	b.setText("Yes");
			            	b.setOnClickListener(new OnClickListener() {
			
								@Override
								public void onClick(View v) {
									// TODO Auto-generated method stub
									AudioManager audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
									audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
								}
			            		
			            	});
			            	Button c = new Button(this);
			            	c.setText("No");
			            	c.setOnClickListener(new OnClickListener() {
			
								@Override
								public void onClick(View v) {
									// TODO Auto-generated method stub
									
								}
			            		
			            	});
		            		y.addView(tv);
		            		y.addView(b);
		            		y.addView(c);
		            		a.setContentView(y);
		        		}
		        	}
		        }
	        }
	    } catch (Exception e) {
        	e.printStackTrace();
        	Dialog FF = new Dialog(this); // To Show Error
        	FF.setTitle("ERROR");
        	TextView FFF = new TextView(this);
        	FFF.setText("Location Error \n" + e);
        	FF.setCanceledOnTouchOutside(true);
        	FF.setContentView(FFF);
        }
        // End Location 
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater startUp = getMenuInflater();
		startUp.inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()) {
		case R.id.refresh:
			webViewEin.reload();
			break;
		}
		return false;
	}    
}